<?php
class Starterkit extends CI_Controller{

    private $limit = 10;
	function  __construct() {
        
		parent::__construct();
        $this->load->model('Starterkit_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation','pagination'));
    }

	function index(){

        $page=$this->uri->segment(3);
	    $batas=10;
	    if(!$page):
	    $offset = 0;
	    else:
	    $offset = $page;
	    endif;

	    $data['nama']="";
	    $postkata = $this->input->post('nama');
	    if(!empty($postkata))
	    {
	        $data['nama'] = $this->input->post('nama');
	        $this->session->set_userdata('pencarian_starterkit', $data['nama']);
	    }
	    else
	    {
	        $data['nama'] = $this->session->userdata('pencarian_starterkit');
	    }
	    $data['starter_kit'] = $this->Starterkit_model->get_list_data($batas,$offset,$data['nama']);
	    $tot_hal = $this->Starterkit_model->tot_hal('barang','strnama',$data['nama']);

	    $config['base_url'] = base_url() . 'starterkit/index/';
	    $config['total_rows'] = $tot_hal->num_rows();
	    $config['per_page'] = $batas;
	    $config['uri_segment'] = 3;
	    $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';
        $this->pagination->initialize($config);
	    $this->pagination->initialize($config);
	    $data["pagination"] =$this->pagination->create_links();
		
	    $this->load->view('admin_views/starterkit/starterkit',$data);
		
    }
	
	function add()
	{
		$this->form_validation->set_rules('intid_week_start', 'Minggu Start', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_week_end', 'Minggu Akhir', 'trim|required|xss_clean');


		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		{
			
                $week = $this->Starterkit_model->selectWeek();
				foreach ($week as $g)
				{
					$data['idw'][]	        = $g->intid_week;
					$data['ide'][]	        = $g->intid_week;
					
				}
			$this->load->view('admin_views/starterkit/add', $data);
                        

		}else {
			$this->Starterkit_model->insert($_POST);
			redirect('starterkit');
		}
	}

    function lookupBarang(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Starterkit_model->selectBarang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
    function edit($intid_starterkit)
		{
			if($_POST==NULL)
			{
                $week = $this->Starterkit_model->selectWeek();
				foreach ($week as $g)
				{
					$data['idw'][]	        = $g->intid_week;
					$data['ide'][]	        = $g->intid_week;
				}
                $datastarterkit= $this->Starterkit_model->select($intid_starterkit);
				foreach ($datastarterkit as $g)
				{
					$data['intid_starterkit']		= $g->intid_starterkit;
					$data['intid_week_start'] 		= $g->intid_week_start;
					$data['intid_week_end'] 		= $g->intid_week_end;
					$data['intid_barang_starterkit']= $g->intid_barang_starterkit;
					$data['intid_barang']  			= $g->intid_barang;
					$data['strnama']				= $g->strnama;
					$data['nama']  					= $g->nama;
				}
				$this->load->view('admin_views/starterkit/edit', $data);
			}else {
				$this->Starterkit_model->update($intid_starterkit);
				redirect('starterkit/index');
			}

		}
	

    function delete($intid_starterkit)
	{
		$this->Starterkit_model->delete($intid_starterkit);
		redirect('starterkit');
	}

}
?>
